import {Component, OnInit, ViewChild} from '@angular/core';
import {adSystemApiUrl} from '../../../../config/api';
import {HttpService} from '../../../lib/service/http.service';
import {ModalDirective} from 'ngx-bootstrap';
import {AddCateogryService} from './add-category.service';

@Component({
  selector: 'app-add-category',
  templateUrl: 'add-category.component.html',
  styleUrls: ['add-category.component.scss'],
  providers: [AddCateogryService]
})
export class AddCategoryComponent implements OnInit {

  type: string;
  message: string;
  isShow = false;

  addFirName: string; // 增加一级类目名称
  addSecName: string; // 增加二级类目名称
  addThirdName: string; // 增加三级类目名称
  firLevel = '';  // 一级类目列表
  secLevel = '';  // 二级类目
  thirdLevel = '';  // 三级类目
  firId: number;  // 选中的一级类目ID
  secId: number;  // 选中的二级类目ID
  thirdId: number;  // 选中的三级类目ID

  editName: string;  // 修改的类目名称
  editId: number;  // 修改的类目ID
  editLevel: number; // 修改级别
  eidtParent: number; // 修改类目的父id

  delId;

  @ViewChild('editModal') public editModal: ModalDirective;
  @ViewChild('delModal') public delModal: ModalDirective;

  constructor(private httpService: HttpService, private addCateogryService: AddCateogryService) {
  }

  ngOnInit() {
    this.getCategoryList(0, 0, 'firLevel');
  }

  // 获取类目列表
  getCategoryList(level: number, parent: number, type: string) {
    const url = `${adSystemApiUrl}/v1/admin/goods_category/list?level=${level}&parent=${parent}&pageSize=200`;
    return this.httpService.httpGetAuth(url)
      .subscribe(
        res => {
          this[type] = res.data.data;
        }
      );
  }

  // 选中类目
  chosedCategory(level: number, id: number) {
    if (level === 0) {
      this.firId = id;
      this.secId = 0;
      this.thirdId = 0;
      this.thirdLevel = null;
      this.getCategoryList(1, id, 'secLevel');
    } else if (level === 1) {
      this.secId = id;
      this.getCategoryList(2, id, 'thirdLevel');
    } else {
      this.thirdId = id;
    }
  }

  // 添加类目
  addCategorySubmit(name: string, level: number, parent: number) {
    const url = `${adSystemApiUrl}/v1/admin/goods_category/add`;
    const data = {name: name, level: level, parent: parent};
    return this.httpService.httpPostAuth(url, data)
      .subscribe(
        res => {
          if (res.code === 2000) {
            this.type = 'success';
            this.message = '添加类目成功';
            this.isShow = true;
            if (level === 0) {
              this.getCategoryList(0, 0, 'firLevel');
            } else if (level === 1) {
              this.getCategoryList(1, this.firId, 'secLevel');
            } else {
              this.getCategoryList(2, this.secId, 'thirdLevel');
            }
          } else {
            this.type = 'error';
            this.message = res.message;
            this.isShow = true;
          }
        }
      );
  }

  // 添加类目
  addCategory(level: number) {
    if (level === 0) {
      if (!this.addFirName) {
        this.type = 'info';
        this.message = '请输入类目名称';
        this.isShow = true;
      } else {
        this.addCategorySubmit(this.addFirName, 0, 0);
      }
    } else if (level === 1) {
      if (!this.addSecName) {
        this.type = 'info';
        this.message = '请输入类目名称';
        this.isShow = true;
        return;
      }
      if (!this.firId) {
        this.type = 'info';
        this.message = '请先选择一级类目';
        this.isShow = true;
        return;
      }
      this.addCategorySubmit(this.addSecName, 1, this.firId);
    } else {
      if (!this.addThirdName) {
        this.type = 'info';
        this.message = '请输入类目名称';
        this.isShow = true;
        return;
      }
      if (!this.firId) {
        this.type = 'info';
        this.message = '请先选择二级类目';
        this.isShow = true;
        return;
      }
      this.addCategorySubmit(this.addThirdName, 2, this.secId);

    }
  }

  // 修改类目名称
  editCategory(name: string, id: number, level: number, parent: number) {
    this.editName = name;
    this.editId = id;
    this.editLevel = level;
    this.eidtParent = parent;
    this.editModal.show();
  }

  // 提交修改类目名称
  editSubmit() {
    const url = `${adSystemApiUrl}/v1/admin/goods_category/edit`;
    const data = {id: this.editId, name: this.editName, level: this.editLevel, parent: this.eidtParent};
    return this.httpService.httpPostAuth(url, data)
      .subscribe(
        res => {
          this.editModal.hide();
          if (res.code === 2000) {
            this.type = 'success';
            this.message = '修改成功';
            this.isShow = true;
            if (this.editLevel === 0) {
              this.getCategoryList(0, 0, 'firLevel');
            } else if (this.editLevel === 1) {
              this.getCategoryList(1, this.firId, 'secLevel');
            } else {
              this.getCategoryList(2, this.secId, 'thirdLevel');
            }
          } else {
            this.type = 'error';
            this.message = res.message;
            this.isShow = true;
          }
        }
      );
  }

  promptIsShow(event: any): void {
    this.isShow = event;
  }

  // 删除属性
  delCategory(id: number) {
    console.log(id);
    this.delId = id;
    this.delModal.show();
  }

  doDelCateory() {
    console.log('del');
    if (this.delId > 0) {
      this.delCategoryById();
    }
  }

  delCategoryById() {
    this.addCateogryService.delCategory(this.delId).subscribe(res => {
      console.log(res);
      if (res.code === 2000) {
        this.type = 'success';
        this.message = '修改成功';
        this.isShow = true;
        this.getCategoryList(2, this.secId, 'thirdLevel');
      } else {
        this.type = 'error';
        this.message = res.message;
        this.isShow = true;
      }
      this.delModal.hide();
    });
  }

}
